<?php

require('config.php');


$language_link=$_GET['id'];

if(($language_link != null && $language_link > 0 ) || (isset($_GET['id']))) {
	$news_menu_left = modelNews::getNewsObject($language_link);
}else{
	redirectFront(LANGUAGE_FOLDER."/index");
}

$html_name = $news_menu_left->url_rewriting.'-'.$language_link.'.html';

	if($news_menu_left->id_news_type > 0) {
		$newstByTypeLink = modelNews::getNewsByUrl('news-list.php?type='.$news_menu_left->id_news_type);
		$news_menu_lefts = modelNews::getNewsMenuleft($newstByTypeLink->language_link);
	}else{
		$newsByType = modelNews::getNewsByUrl('news-list.php');
		$news_menu_lefts = modelNews::getNewsMenuleft($newsByType->language_link);
	}
	
	if($news_menu_lefts and $news_menu_lefts->id_menu_left_tree > 0){
		if($news_menu_lefts->need_login == STATUS_NEED_CONNECTION){
			if(!isset($_SESSION['id_user'])){
				$_SESSION['referer_connexion'] =  "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
				redirectFront('connexion');
			}
		}
	}else redirectFront('connexion');

$page = new rootLayoutPage();
$page->addJavaScript('module.js');

//-- Menu Page ----------------------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menuPage->setTable('news');
$menuPage->setIdShow($news_menu_lefts->id_menu_left_tree);
$menu->setIdShow($news_menu_lefts->id_menu_left_tree);
$menu->pushMenu($menuPage);

$page = new rootLayoutPage();
$page->setMenuPage($menu);

//--------Menu indicator--------------//
/*$menuIndictor = new layoutMenuIndicator();
$menuIndictor->idTree($news_menu_lefts->id_menu_left_tree);
$menuIndictor->title($news_menu_lefts->title);
$page->setMenuIndicator($menuIndictor);*/
//--------Menu Header-----------------//
//************************//
$arr = array();

$getListUrlNews = modelNews::getListUrlNews($language_link);
foreach($getListUrlNews AS $key=>$Url){
	$arr[$Url->language_code]=$Url->url_rewriting.'-'.$Url->language_link.'.html';
}
$header = new layoutHeader();
$header->setArrayUrl($arr);
$header->setIdShow($news_menu_lefts->id_menu_left_tree);
$page->setHeader($header);

//************************//

$page->setMenuPage($menu);
$page->setBaliseTitle($news_menu_left->meta_title);
$page->setDescription($news_menu_left->meta_description);
$extension = filename_extension($_SERVER['REQUEST_URI']);
$page->setCanonical($html_name);

$tpl_file='news-detail.tpl';
$news_detail = new flyLayout(REP_TPL . $tpl_file);
$news_detail->start();


	$news_detail->setVariable('page_class','10');
	
	//*********************//
	/*if(!$news_menu_left->id_menu_left_tree){
		if($news_menu_left->id_news_type>0){
			$new_title = modelNews::getNewsByUrl('news-list.php?type='.$news_menu_left->id_news_type);
			$getNewsByType = modelNews::getNewsByType($news_menu_left->id_news_type);
			$type_name = 'news_type_'.$_SESSION['language_code'];
			$new_type_title = $getNewsByType->$type_name;
		}else{
			$new_title = modelNews::getNewsByUrl('news-list.php');
			$new_type_title = $new_title->title;
		}
		$news_detail->setVariable('language_folder', LANGUAGE_FOLDER);
		$url = $new_title->url ;
		$news_detail->setVariable('url', $url);
		
		$news_detail->setVariable('title', $new_type_title);
		$news_detail->setVariable('meta_title', $new_title->meta_title);		
		$news_detail->setVariable('block_show', 'true');
		$news_detail->showBlock('not_in_tree');
	}*/
	
	if($news_menu_lefts){
			$listPage = modelMenuLeft::completePathFromRoot($news_menu_lefts->id_menu_left_tree);
        	//$listPage = modelMenuLeft::completePathFromRoot($this->id_tree);
			foreach($listPage as $key => $pageAriane) {

				if($listPage->index() == 0) continue; // pour sauter la racine

				//if($listPage->size() == $key+1) break; //ignor current page

				if($listPage->index() == 0)$news_detail->hideBlock('bar1');else $news_detail->showBlock('bar1');
				if($key<$listPage->size()-2) $news_detail->showBlock('bar');
				//if($key == $listPage->size()-2)$news_detail->hideBlock('bar');


				$data = modelMenuLeft::getDataFromContentPage($pageAriane->table_name,$pageAriane->language_link,$pageAriane->language_code);
					if($data){
						$news_detail->setVariable('nom_news', $data->title);
						$url_page = $data->url_rewriting.'-'.$data->language_link.'.html';
						$news_detail->setVariable('url_news', $data->url?$data->url:$url_page);
						$news_detail->setVariable('title_news', $data->meta_title);
					}
				$news_detail->parseList('list_new');
			}
			$news_detail->showBlock('indicator');
	}
	$page->setIndex($news_menu_left->index);
	$page->setFollow($news_menu_left->follow);
	$newContent = affiche($news_menu_left->content, 'tcode');
	
	preg_match('/\.\.\./',$news_menu_left->title,$match_origin);
	preg_match('/\.\.\./',affiche($news_menu_left->title,'sort_title_news'),$match);
	if($match_origin[0] !='...'){
		$show_title_news = $match[0]=='...'?$news_menu_left->title:'';
	}
	
	$news_detail->setVariable('title_news', affiche($news_menu_left->title,$_SESSION['language_code']=='kh'?'sort_title_news_kh':'sort_title_news'));
	$news_detail->setVariable('show_title_news', $show_title_news);
	$news_detail->setVariable('news',$newContent);

//*******Nivigation news*******************//
$getNewsNext = modelNews::getNewsNavigate(NAVIGATE_NEXT,$news_menu_left->id_news_type,$news_menu_left->date_news,ITEM_NEW_PER_PAGE_NEWS);

$limit_old = ITEM_PER_PAGE_NEWS - $getNewsNext->size();
$getNewsPrev = modelNews::getNewsNavigate(NAVIGATE_PREVIOUS,$news_menu_left->id_news_type,$news_menu_left->date_news,$limit_old);
// prev link
$getNewsPrev_1 = $getNewsPrev->nextObject();
if($getNewsPrev_1){	
	$url_prv = $getNewsPrev_1->url_rewriting.'-'.$getNewsPrev_1->language_link.'.html';
	$news_detail->setVariable('url_prv',$url_prv);
	$news_detail->setVariable('meta_title_prv',$getNewsPrev_1->meta_title);
	$news_detail->showBlock('news_navigator_prv');
}else{
	$news_detail->hideBlock('news_navigator_prv');
}
// next link
$num_newsNext = $getNewsNext->nextObject();
if($num_newsNext){
			$url_next = $num_newsNext->url_rewriting.'-'.$num_newsNext->language_link.'.html';
			$news_detail->setVariable('url_next',$url_next);
			$news_detail->setVariable('meta_title_next',$num_newsNext->meta_title);
			$news_detail->showBlock('news_navigator_next');
}else{
	$news_detail->hideBlock('news_navigator_next');
}
// block list same level
foreach($getNewsNext AS $key=>$news){
	$news_detail->setVariable('nom_news_same_level', $news->title);	
	$url_news_same_level = $news->url_rewriting.'-'.$news->language_link.'.html';
	$news_detail->setVariable('url_news_same_level', $url_news_same_level);
	$news_detail->setVariable('title_news_same_level', $news->meta_title);
	$news_detail->setVariable('language_folder', LANGUAGE_FOLDER);
	//$news_detail->parseList('list_news_same_level');
	$news_detail->parseReverseList('list_news_same_level');
	$news_detail->showBlock('list_pages_same_level');
}
foreach($getNewsPrev AS $key=>$news){
	$news_detail->setVariable('nom_news_same_level', $news->title);	
	$url_news_same_level = $news->url_rewriting.'-'.$news->language_link.'.html';
	$news_detail->setVariable('url_news_same_level', $url_news_same_level);
	$news_detail->setVariable('title_news_same_level', $news->meta_title);
	$news_detail->setVariable('language_folder', LANGUAGE_FOLDER);
	$news_detail->parseList('list_news_same_level');
	$news_detail->showBlock('list_pages_same_level');	
}

$news_detail->stop();
$page->setBody($news_detail);

$page->display();

?>